package com.tkjt.pms.modules.kpiadmin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.*;
import com.tkjt.pms.common.utils.DoubleUtil;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import com.tkjt.pms.modules.common.mapper.*;
import com.tkjt.pms.modules.kpiadmin.mapper.KpiAdminMapper;
import com.tkjt.pms.modules.kpiadmin.model.KaiProgressDetailModel;
import com.tkjt.pms.modules.kpiadmin.model.KaiProgressViewModel;
import com.tkjt.pms.modules.kpiadmin.service.KpiAdminBizService;
import com.tkjt.pms.server.common.DepartAndInfoTool;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Service
@Transactional
public abstract class KpiAdminBizServiceBase implements KpiAdminBizService {
    @Autowired
    KpiAdminMapper kpiAdminMapper;
    @Autowired
    TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    TkIndexValueAuditMapper tkIndexValueAuditMapper;
    @Autowired
    TkIndexReportMapper tkIndexReportMapper;
    @Autowired
    TkIndexInfoMapper tkIndexInfoMapper;
    @Autowired
    private DepartAndInfoTool departAndInfoTool;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TkDepartGroupMapper tkDepartGroupMapper;


    @Override
    public KaiProgressViewModel getKaiProgressView(HttpServletRequest request,String dataType) {
        KaiProgressViewModel model=new KaiProgressViewModel();
        String yearMonth = request.getParameter("yearMonth");
        if(StringUtils.isEmpty(yearMonth)){
            yearMonth=EsDateUtils.getIndexYearMonth(new Date());
        }
        model.setPnodes(getPnodes(yearMonth,dataType));
        model.setCurrentMonth(yearMonth);
        return model;
    }

    @Override
    public KaiProgressDetailModel getKaiProgressDetail(HttpServletRequest request,String dataType) {
        String yearMonth = request.getParameter("yearMonth");
        if(StringUtils.isEmpty(yearMonth)){
            yearMonth=EsDateUtils.getIndexYearMonth(new Date());
        }
        KaiProgressDetailModel model=new KaiProgressDetailModel();
        //当前需要统计的月份
        String lastMonthStr = yearMonth.substring(4);
        if(lastMonthStr.startsWith("0")){
            lastMonthStr = lastMonthStr.substring(1);
        }
        int month = Integer.valueOf(lastMonthStr);
//        //当前需要统计的年月 例:202001
//        String indexYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        //第一列
        List<KaiProgressDetailModel.ItemsBean> items=new ArrayList<>();
        List<Map<String,String>> values=new ArrayList<>();

        int sumT1=0; int sumR1=0; int sumT2=0; int sumR2=0;
        int sumTwoWayT1=0;int sumTwoWayR1=0;int sumTwoWayT2=0;int sumTwoWayR2=0;
        KaiProgressDetailModel.ItemsBean itemsBean=new KaiProgressDetailModel.ItemsBean();
        //itemsBean.setName("部门和单位(总填数/已填数)");
        itemsBean.setName("部门和单位");
        itemsBean.setCode("CGTYPE000");
        items.add(itemsBean);
        Map<String,String> yixianMap=new HashMap<>();
        yixianMap.put("CGTYPE000","一线单位");
        Map<String,String> benbuMap=new HashMap<>();
        benbuMap.put("CGTYPE000","养康本部");
        Map<String,Object> param = new HashMap<>();
        //考核、权重、加减分项
        param.put("noIndexType","0");
        param.put("indexEdit","1");
        param.put("groupByIndexCode","groupByIndexCode");
        //所有指标
        List<TkIndexInfo> indexInfoList = tkIndexInfoMapper.selectByArrtsParam(param);
        String[] roleCodes = {"ykgm","ykkpiowner"};
        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        QueryWrapper<TkDepartGroup> departGroupQuery = new QueryWrapper<>();
        departGroupQuery.eq("INDEX_GROUP","2");
        departGroupQuery.ne("DEPART_ATTR","ATRV0000");
        List<TkDepartGroup> departs = tkDepartGroupMapper.selectList(departGroupQuery);
        List<String> cDepartCodes = new ArrayList<>();
        for(TkDepartGroup tkDepartGroup : departs){
            cDepartCodes.add(tkDepartGroup.getIndexDepart());
        }
        departGroupQuery = new QueryWrapper<>();
        departGroupQuery.eq("INDEX_GROUP","1");
        departGroupQuery.ne("DEPART_ATTR","ATRV0000");
        departs = tkDepartGroupMapper.selectList(departGroupQuery);
        List<String> gDepartCodes = new ArrayList<>();
        for(TkDepartGroup tkDepartGroup : departs){
            gDepartCodes.add(tkDepartGroup.getIndexDepart());
        }
        for (TkIndexInfo t : indexInfoList) {
            String indexCode = t.getIndexCode();
            param = new HashMap<>();
            param.put("indexEdit","1");
            param.put("indexCode",indexCode);
            //所有指标
            List<TkIndexInfo> childInfos = tkIndexInfoMapper.selectByArrtsParam(param);
            List<String> indexValueCodes = new ArrayList<>();
            for(TkIndexInfo tkIndexInfo : childInfos){
                indexValueCodes.add(tkIndexInfo.getIndexValueCode());
            }
            QueryWrapper<TkIndexValue> qw = new QueryWrapper<>();
            qw.notIn("INDEX_EXECUTOR", userNames);
            qw.in("INDEX_VALUE_CODE", indexValueCodes);
            qw.in("INDEX_DEPART",cDepartCodes); //一线单位
            qw.eq("DATA_TYPE", dataType);
            Integer t1 = tkIndexValueMapper.selectCount(qw);
            //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
            //INDEX_VALUE_STATUS >= 2 表示已填报
            qw.ge("INDEX_VALUE_STATUS",month);
            Integer r1 = tkIndexValueMapper.selectCount(qw);
            qw = new QueryWrapper<>();
            qw.notIn("INDEX_EXECUTOR", userNames);
            qw.in("INDEX_VALUE_CODE", indexValueCodes);
            qw.in("INDEX_DEPART",gDepartCodes); //事业群
            qw.eq("DATA_TYPE", dataType);
            Integer t2 = tkIndexValueMapper.selectCount(qw);
            //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
            //INDEX_VALUE_STATUS >= 2 表示已填报
            qw.ge("INDEX_VALUE_STATUS",month);
            Integer r2 = tkIndexValueMapper.selectCount(qw);

            yixianMap.put(indexCode,r1+"/"+t1);
            benbuMap.put(indexCode,r2+"/"+t2);
            //指标编码及名称
            KaiProgressDetailModel.ItemsBean itemsBean2=new KaiProgressDetailModel.ItemsBean();
            itemsBean2.setName(t.getIndexName());
            itemsBean2.setCode(t.getIndexCode());
            items.add(itemsBean2);
            sumT1 +=t1; sumR1 +=r1;sumT2 +=t2; sumR2 +=r2;
        }

        QueryWrapper<TkIndexValue> qw = new QueryWrapper<>();
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
        userNames.add("system");
        qw.notIn("INDEX_EXECUTOR",  userNames);
        qw.in("INDEX_VALUE_CODE", departAndInfoTool.getIndexValueCodesByOrgGroup("C",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A01"); //一线单位
        qw.eq("DATA_TYPE", "kpi");
        Integer t1 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        Integer r1 = tkIndexValueMapper.selectCount(qw);
        qw = new QueryWrapper<>();
        qw.notIn("INDEX_EXECUTOR",  userNames);
        qw.in("INDEX_VALUE_CODE", departAndInfoTool.getIndexValueCodesByOrgGroup("G",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A02"); //事业群
        qw.eq("DATA_TYPE", "kpi");
        Integer t2 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        Integer r2 = tkIndexValueMapper.selectCount(qw);
//        sumT1 +=t1; sumR1 +=r1;sumT2 +=t2; sumR2 +=r2;//不需要加入总数
        sumTwoWayT1 +=t1; sumTwoWayR1 +=r1;sumTwoWayT2 +=t2; sumTwoWayR2 +=r2;
        String indexCode = "twowayScore";
        String indexName = "双向评分";
        yixianMap.put(indexCode,sumTwoWayR1+"/"+sumTwoWayT1);
        benbuMap.put(indexCode,sumTwoWayR2+"/"+sumTwoWayT2);
        
        //双向评分要在页面列表第三列显示
        KaiProgressDetailModel.ItemsBean itemsBean2=new KaiProgressDetailModel.ItemsBean();
        itemsBean2.setName(indexName);
        itemsBean2.setCode(indexCode);
        items.add(1, itemsBean2);
        
        //双进度率要在页面列表第二列显示
        indexCode="CGPERCENT";
        yixianMap.put(indexCode,DoubleUtil.divPercent(sumR1+sumTwoWayR1,sumT1+sumTwoWayT1));
        benbuMap.put(indexCode,DoubleUtil.divPercent(sumR2+sumTwoWayR2,sumT2+sumTwoWayT2));
        itemsBean=new KaiProgressDetailModel.ItemsBean();
        itemsBean.setName("进度率");
        itemsBean.setCode(indexCode);
        items.add(1, itemsBean);

//        //系统时间为奇数时,增加动态指标
//        //正式上线时需要去掉
//        int minutes = EsDateUtils.getMinutes(new Date());
//        if(minutes%2 ==1){
//            indexCode="TESTCODE";
//            yixianMap.put(indexCode,60+"/"+minutes);
//            benbuMap.put(indexCode,60+"/"+minutes);
//            itemsBean=new KaiProgressDetailModel.ItemsBean();
//            itemsBean.setName("测试指标)");
//            itemsBean.setCode(indexCode);
//            items.add(itemsBean);
//        }
        model.setItems(items);
        values.add(yixianMap);
        values.add(benbuMap);
        model.setValues(values);
        return model;

    }


    private List<KaiProgressViewModel.PnodesBean> getPnodes(String indexYearMonth,String dataType){
        //当前需要统计的月份
        String lastMonthStr = indexYearMonth.substring(4);
        int month = 0;
        if(lastMonthStr.startsWith("0")){
            month = Integer.valueOf(lastMonthStr.substring(1));
        }
        month = Integer.valueOf(lastMonthStr);
        String year=indexYearMonth.substring(0,4);
        List<KaiProgressViewModel.PnodesBean> list=new ArrayList<>();
        //1 双向评分  质量改进情况 得分  重点工作完成 得分 除去ykgm和kpiadmin
        KaiProgressViewModel.PnodesBean pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("twoWayScore");
        pnodesBean.setName("双向评分");
        pnodesBean.setPseq("1");
        pnodesBean.setTodoUrl("");
        QueryWrapper<TkIndexValue> qw = new QueryWrapper<>();
        String[] roleCodes = {"ykgm","ykkpiowner"};
        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        userNames.add("system");
        qw.notIn("INDEX_EXECUTOR", userNames);
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
        qw.in("INDEX_CODE", departAndInfoTool.getIndexCodesByOrgGroup("C",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A01"); //一线单位
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE","kpi");
        Integer t1=tkIndexValueMapper.selectCount(qw);//总填报数量
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        Integer r1 = tkIndexValueMapper.selectCount(qw);//完成填报数量
        qw = new QueryWrapper<>();
        qw.notIn("INDEX_EXECUTOR", userNames);
        qw.in("INDEX_CODE", departAndInfoTool.getIndexCodesByOrgGroup("G",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A02"); //养康本部
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE","kpi");
        Integer t2=tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        Integer r2 = tkIndexValueMapper.selectCount(qw);
        pnodesBean.setPtips(getPtips(r1,t1,r2,t2));
        pnodesBean.setStatus(getStatus(r1,t1,r2,t2));
        list.add(pnodesBean);
        String status1=pnodesBean.getStatus();
        //2 绩效填报  非双向评分 非小计合计
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("performanceReporting");
        pnodesBean.setName("绩效填报");
        pnodesBean.setPseq("1");
        pnodesBean.setTodoUrl("/kpi/KpiFill");
        qw = new QueryWrapper<>();
        Map<String,Object> param = new HashMap<>();
        param.put("indexEdit","1");
        param.put("indexGroup","C");
        //所有指标
        List<TkIndexInfo> infos = tkIndexInfoMapper.selectByArrtsParam(param);
        List<String> indexValueCodes = new ArrayList<>();
        for(TkIndexInfo info : infos){
            indexValueCodes.add(info.getIndexValueCode());
        }
        //qw.notIn("INDEX_EXECUTOR", userNames);
        qw.in("INDEX_VALUE_CODE", indexValueCodes);
        QueryWrapper<TkDepartGroup> departGroupQuery = new QueryWrapper<>();
        departGroupQuery.eq("INDEX_GROUP","2");
        departGroupQuery.ne("DEPART_ATTR","ATRV0000");
        List<TkDepartGroup> departs = tkDepartGroupMapper.selectList(departGroupQuery);
        List<String> departCodes = new ArrayList<>();
        for(TkDepartGroup tkDepartGroup : departs){
            departCodes.add(tkDepartGroup.getIndexDepart());
        }
        qw.in("INDEX_DEPART",departCodes); //一线单位
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        t1 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        r1 = tkIndexValueMapper.selectCount(qw);
        param = new HashMap<>();
        param.put("indexEdit","1");
        param.put("indexGroup","G");
        //所有指标
        infos = tkIndexInfoMapper.selectByArrtsParam(param);
        indexValueCodes = new ArrayList<>();
        for(TkIndexInfo info : infos){
            indexValueCodes.add(info.getIndexValueCode());
        }
        qw = new QueryWrapper<>();
        //qw.notIn("INDEX_EXECUTOR", userNames);
        qw.in("INDEX_VALUE_CODE", indexValueCodes);
        departGroupQuery = new QueryWrapper<>();
        departGroupQuery.eq("INDEX_GROUP","1");
        departGroupQuery.ne("DEPART_ATTR","ATRV0000");
        departs = tkDepartGroupMapper.selectList(departGroupQuery);
        departCodes = new ArrayList<>();
        for(TkDepartGroup tkDepartGroup : departs){
            departCodes.add(tkDepartGroup.getIndexDepart());
        }
        qw.in("INDEX_DEPART",departCodes); //事业群
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        t2 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        r2 = tkIndexValueMapper.selectCount(qw);
        pnodesBean.setPtips(getPtips(r1,t1,r2,t2));
        pnodesBean.setStatus(getStatus(r1,t1,r2,t2));
        list.add(pnodesBean);
        String status2=pnodesBean.getStatus();

        //3 汇总评分  kpiadmin 质量改进
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("summaryScore");
        pnodesBean.setName("汇总评分");
        pnodesBean.setPseq("2");
        pnodesBean.setTodoUrl("/kpi/KpiGrade");
        qw = new QueryWrapper<>();
        infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
        roleCodes = new String[]{"ykkpiowner"};
        userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        qw.eq("INDEX_EXECUTOR",  userNames.get(0));
        qw.in("INDEX_CODE", departAndInfoTool.getIndexCodesByOrgGroup("C",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A01"); //一线单位
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        t1 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        r1 = tkIndexValueMapper.selectCount(qw);
        qw = new QueryWrapper<>();
        qw.eq("INDEX_EXECUTOR",  userNames.get(0));
        qw.in("INDEX_CODE", departAndInfoTool.getIndexCodesByOrgGroup("G",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A02"); //事业群
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        t2 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        r2 = tkIndexValueMapper.selectCount(qw);

        if("finished".equals(status1) && "finished".equals(status2)){
            pnodesBean.setPtips(getPtips(r1,t1,r2,t2));
            pnodesBean.setStatus(getStatus(r1,t1,r2,t2));
        }else{
            pnodesBean.setStatus("waiting");
        }
        list.add(pnodesBean);
        String status=pnodesBean.getStatus();

        //4 总经理评分  ykgm   重点工作完成
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("gmScore");
        pnodesBean.setName("总经理评分");
        pnodesBean.setPseq("3");
        pnodesBean.setTodoUrl("");
        qw = new QueryWrapper<>();
        roleCodes = new String[]{"ykgm"};
        userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        qw.eq("INDEX_EXECUTOR",  userNames.get(0));
        qw.in("INDEX_CODE", departAndInfoTool.getIndexCodesByOrgGroup("C",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A01"); //一线单位
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        t1 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        r1 = tkIndexValueMapper.selectCount(qw);
        qw = new QueryWrapper<>();
        qw.eq("INDEX_EXECUTOR",  userNames.get(0));
        qw.in("INDEX_CODE", departAndInfoTool.getIndexCodesByOrgGroup("G",infoAttrs));
        qw.likeRight("INDEX_DEPART","A01A01A02"); //事业群
        qw.eq("YEAR",year);
        qw.eq("DATA_TYPE",dataType);
        t2 = tkIndexValueMapper.selectCount(qw);
        //INDEX_VALUE_STATUS == 2   1月2月已填报   2月以后未填报
        //INDEX_VALUE_STATUS >= 2 表示已填报
        qw.ge("INDEX_VALUE_STATUS",month);
        r2 = tkIndexValueMapper.selectCount(qw);

        if("finished".equals(status)){
            pnodesBean.setPtips(getPtips(r1,t1,r2,t2));
            pnodesBean.setStatus(getStatus(r1,t1,r2,t2));
        }else{
            pnodesBean.setStatus("waiting");
        }
//        pnodesBean.setStatus(getStatus(r1,t1,r2,t2));
        list.add(pnodesBean);
        status=pnodesBean.getStatus();

        //5 提交审核申请
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("auditApplication");
        pnodesBean.setName("提交审核申请");
        pnodesBean.setPseq("4");
        pnodesBean.setTodoUrl("/kpi/KpiCalc");
        
        QueryWrapper<TkIndexValueAudit> qwAudit=new QueryWrapper<>();
        qwAudit.eq("INDEX_GROUP","A01A01A01");//一线单位
        qwAudit.eq("INDEX_VALUE_Y",year);
        qwAudit.eq("INDEX_VALUE_M",lastMonthStr);
        qwAudit.eq("DATA_TYPE",dataType);
        TkIndexValueAudit tkIndexValueAudit = tkIndexValueAuditMapper.selectOne(qwAudit);
        List<KaiProgressViewModel.PnodesBean.PtipsBean> ptips=new ArrayList<>();
        //一线单位评分率
        KaiProgressViewModel.PnodesBean.PtipsBean ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
        ptipsBean.setKey("一线单位");
        ptipsBean.setValue(getAditStautsNum(tkIndexValueAudit));
        ptips.add(ptipsBean);
        TkIndexValueAudit tempIndexValueAudit5=tkIndexValueAudit;
        qwAudit=new QueryWrapper<>();
        qwAudit.eq("INDEX_GROUP","A01A01A02");//本部各部门
        qwAudit.eq("INDEX_VALUE_Y",indexYearMonth.substring(0,4));
        qwAudit.eq("INDEX_VALUE_M",indexYearMonth.substring(4));
        qwAudit.eq("DATA_TYPE",dataType);
        tkIndexValueAudit = tkIndexValueAuditMapper.selectOne(qwAudit);
        ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
        ptipsBean.setKey("本部各部门");
        ptipsBean.setValue(getAditStautsNum(tkIndexValueAudit));
        ptips.add(ptipsBean);
        pnodesBean.setPtips(ptips);
        pnodesBean.setStatus(getAditStauts5(tempIndexValueAudit5,tkIndexValueAudit));
        //如果审核申请已提交，之前节点状态都更新为finished
        if(!pnodesBean.getStatus().equals("waiting")){
            for(int i = 0 ; i < list.size() ; i++){
                list.get(i).setStatus("finished");
            }
        }
        list.add(pnodesBean);
        status=pnodesBean.getStatus();
        //6 绩效审核通过
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("auditPass");
        pnodesBean.setName("绩效审核通过");
        pnodesBean.setPseq("5");
        pnodesBean.setTodoUrl("/kpi/KpiAuditHistory");
        qwAudit=new QueryWrapper<>();
        qwAudit.eq("INDEX_GROUP","A01A01A01");//一线单位
        qwAudit.eq("INDEX_VALUE_Y",indexYearMonth.substring(0,4));
        qwAudit.eq("INDEX_VALUE_M",indexYearMonth.substring(4));
        qwAudit.eq("DATA_TYPE",dataType);
         tkIndexValueAudit = tkIndexValueAuditMapper.selectOne(qwAudit);
        TkIndexValueAudit tempIndexValueAudit6=tkIndexValueAudit;
        ptips=new ArrayList<>();
        //一线单位评分率
        ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
        ptipsBean.setKey("一线单位");
        ptipsBean.setValue(getAditStautsNum(tkIndexValueAudit));
        ptips.add(ptipsBean);
        qwAudit=new QueryWrapper<>();
        qwAudit.eq("INDEX_GROUP","A01A01A02");//本部各部门
        qwAudit.eq("INDEX_VALUE_Y",indexYearMonth.substring(0,4));
        qwAudit.eq("INDEX_VALUE_M",indexYearMonth.substring(4));
        qwAudit.eq("DATA_TYPE",dataType);
        tkIndexValueAudit = tkIndexValueAuditMapper.selectOne(qwAudit);
        ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
        ptipsBean.setKey("本部各部门");
        ptipsBean.setValue(getAditStautsNum(tkIndexValueAudit));
        ptips.add(ptipsBean);
        if("finished".equals(status)){
            pnodesBean.setPtips(ptips);
            pnodesBean.setStatus(getAditStauts6(tempIndexValueAudit6,tkIndexValueAudit));
        }else{
            pnodesBean.setStatus("waiting");
        }
        list.add(pnodesBean);
        status = pnodesBean.getStatus();
        //7 生成绩效报表
        ptips=new ArrayList<>();
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("calcuPerformance");
        pnodesBean.setName("生成绩效报表");
        pnodesBean.setPseq("6");
        pnodesBean.setTodoUrl("/scoring/report");
        QueryWrapper<TkIndexReport> qwReport = new QueryWrapper<>();
        // 'RHC:医院&社区;RDP:养康本部;RHCH:医院&社区(领导);RDPH:养康本部'
        qwReport.in("REPORT_CODE","RDPH","RDP");
        qwReport.eq("REPORT_STATUS","3");// 0:有错误; 1:已就绪; 2:生成中; 3:已生成;
        qwReport.eq("DATA_TYPE",dataType);
        qwReport.eq("INDEX_YEAR_MONTH",indexYearMonth);
        List<TkIndexReport> RDPIndexReports = tkIndexReportMapper.selectList(qwReport);
        qwReport = new QueryWrapper<>();
        // 'RHC:医院&社区;RDP:养康本部;RHCH:医院&社区(领导);RDPH:养康本部'
        qwReport.in("REPORT_CODE","RHCH","RHC");
        qwReport.eq("REPORT_STATUS","3");// 0:有错误; 1:已就绪; 2:生成中; 3:已生成;
        qwReport.eq("DATA_TYPE",dataType);
        qwReport.eq("INDEX_YEAR_MONTH",indexYearMonth);
        List<TkIndexReport> RHCIndexReports = tkIndexReportMapper.selectList(qwReport);
        if("finished".equals(status)){
            int statusNum=0;
            ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
            ptipsBean.setKey("本部各部门");
            if(RDPIndexReports != null && RDPIndexReports.size() == 2){
                ptipsBean.setValue("已完成!");
                statusNum++;
            }else{
                ptipsBean.setValue("生成中...");
            }
            ptips.add(ptipsBean);
            ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
            ptipsBean.setKey("一线单位");
            if(RHCIndexReports != null && RHCIndexReports.size() == 2){
                ptipsBean.setValue("已完成!");
                statusNum++;
            }else{
                ptipsBean.setValue("生成中...");
            }
            ptips.add(ptipsBean);
            pnodesBean.setPtips(ptips);
            pnodesBean.setStatus(statusNum == 2 ? "finished":"ongoing");
        }else{
            pnodesBean.setStatus("waiting");
        }
        list.add(pnodesBean);
        status=pnodesBean.getStatus();
        //8 汇总评分
        pnodesBean=new KaiProgressViewModel.PnodesBean();
        pnodesBean.setCode("donePerformance");
        pnodesBean.setName("绩效完成("+indexYearMonth.substring(0,4)+"年"+indexYearMonth.substring(4)+"月)");
        pnodesBean.setPseq("7");
        pnodesBean.setTodoUrl("");
        if(!"ongoing".equals(status) ){
            pnodesBean.setStatus(status);
        }else{
            pnodesBean.setStatus("waiting");
        }
        list.add(pnodesBean);
        return list;
    }

    /**
     * 计算评分率
     */
    private List<KaiProgressViewModel.PnodesBean.PtipsBean> getPtips(int r1,int t1,int r2,int t2){
        List<KaiProgressViewModel.PnodesBean.PtipsBean> ptips=new ArrayList<>();
        //一线单位评分率
        KaiProgressViewModel.PnodesBean.PtipsBean ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
        ptipsBean.setKey("一线单位评分率");
        ptipsBean.setValue(DoubleUtil.divPercent(r1, t1));
        ptips.add(ptipsBean);
        //本部部门评分率
        ptipsBean=new KaiProgressViewModel.PnodesBean.PtipsBean();
        ptipsBean.setKey("本部部门评分率");
        ptipsBean.setValue(DoubleUtil.divPercent(r2, t2));
        ptips.add(ptipsBean);
        return ptips;
    }

    private String getStatus(int r1,int t1,int r2,int t2){
        if(r1 == t1 && r2 == t2){
            return "finished";
        }else if(r1 == 0 && r2 == 0){
            return "waiting";
        }else{
            return "ongoing";
        }
    }

    private String getAditStauts(TkIndexValueAudit tkIndexValueAudit){
        if(tkIndexValueAudit == null){
            return "waiting";
        }
        switch (tkIndexValueAudit.getAuditStatus()){
            case "1":
            case "2":
                return "ongoing";
            case "3":return "finished";
//            case "4":return "waiting";
            default:return "waiting";
        }
    }

    private String getAditStautsNum(TkIndexValueAudit tkIndexValueAudit){
        if(tkIndexValueAudit == null){
            return "待提交";
        }
        switch (tkIndexValueAudit.getAuditStatus()){
            case "0":return "已初审";
            case "1":return "待审核";
            case "2":return "审核中";
            case "3":return "已审核";
            case "4":return "已退回";
            default:return "待提交";
        }
    }

//     0:有错误; 1:已就绪; 2:生成中; 3:下载中; 4:已下载
    private String getReportStatus(Integer status){
        String report;
        switch (status){
            case 0:report="有错误";break;
            case 1:report="已就绪";break;
            case 2:report="生成中";break;
            case 3:report="下载中";break;
            case 4:report="已下载";break;
            default:report="未知";
        }
        return report;
    }

    /**
     * 审批状态(1:待申请；2:审核中; 3:已审核; 4:已退回)',
     */
    private String getAditStauts5(TkIndexValueAudit t1,TkIndexValueAudit t2){
        if(t1 == null){return "waiting";}
        if(t2 == null){return "waiting";}
        if("1".equals(t1.getAuditStatus()) && "1".equals(t2.getAuditStatus())  ){
            return "waiting";
        }else if("1".equals(t1.getAuditStatus()) || "1".equals(t2.getAuditStatus()) || "4".equals(t1.getAuditStatus()) || "4".equals(t2.getAuditStatus())){
            return "ongoing";
        }else{
            return "finished";
        }

    }
    private String getAditStauts6(TkIndexValueAudit t1,TkIndexValueAudit t2){
        if(t1 == null){t1=new TkIndexValueAudit();}
        if(t2 == null){t2=new TkIndexValueAudit();}
        if("3".equals(t1.getAuditStatus()) && "3".equals(t2.getAuditStatus())  ){
            return "finished";
        }else if(!"3".equals(t1.getAuditStatus()) && !"3".equals(t2.getAuditStatus())){
            return "waiting";
        }else{
            return "ongoing";
        }

    }

}
